package com.vvv.designpattern.synchronizedpattern;

/**
 * @author vvv
 * @date 2023-10-17 08 20
 * discription 固定运行顺序之wait notify版
 * 先输出2 再输出1
 */
public class FixedWaitAndNotify {
    public static final Object lock = new Object();
    public static boolean is2Ran = false;
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                while (!is2Ran) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println(1);
            }
        }, "t1");
        Thread t2 = new Thread(() -> {
            synchronized (lock) {
                System.out.println(2);
                is2Ran = true;
                lock.notify();
            }
        }, "t2");
        t1.start();
        t2.start();

    }
}
